/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.model;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import org.internna.iwebjtracker.server.scm.model.ItemData;
import static org.internna.iwebmvc.utils.StringUtils.hasText;

/**
 * Simple bean to store item data.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class ItemDataImpl implements ItemData {

    private final byte[] data;
    private final String contents;

    public ItemDataImpl(byte[] data) {
        this.data = Arrays.copyOf(data, data.length);
        this.contents = null;
    }

    public ItemDataImpl(String contents) {
        this.data = null;
        this.contents = contents;
    }

    @Override public InputStream getBinaryData() {
        return new ByteArrayInputStream(data);
    }

    @Override public String getReadableContent() {
        return contents;
    }

    @Override public boolean isASCII() {
        return hasText(contents);
    }

    @Override public boolean isBinary() {
        return data != null;
    }

}
